package com.uzai.console.service.cms.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.entity.SysDomain;
import com.uzai.console.mapper.SysDomainMapper;
import com.uzai.console.service.cms.CmsParamReplaceService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2022年08月23日 16:56
 */
@Service
public class CmsParamReplaceServiceImpl implements CmsParamReplaceService {

    Logger logger = LoggerFactory.getLogger(CmsParamReplaceServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SysDomainMapper sysDomainMapper;

    /**
     * 获取CMS商品列表
     * @author liuqi
     * @date 2022/8/23 17:12
     * @param merId
     * @param userId
     * @param deviceUniqueId
     * @return java.lang.String
     */
    @Override
    public String getCmsGoodListUrl(Long merId, Long userId, Long deviceUniqueId) {
        String cmsGoodListUrl = "";
        try {

            String key = String.format(RedisCacheKeyEnum.CMS_BASE_CONFIG.getKey(), merId);
            String cmsConfigStr = (String) redisTemplate.opsForValue().get(key);
            String mallDomain = "0";
            if (StringUtils.isNotBlank(cmsConfigStr)){
                JSONObject cmsConfig = JSONObject.parseObject(cmsConfigStr);
                mallDomain = cmsConfig.getString("mallDomain");
            }

            String domain = null;
            //判断域名是否以http开头
            if (mallDomain.startsWith("http")){
                domain = mallDomain;
            }else {//不是以http开头，则需要从数据库里查询
                Long domainId = 0L;
                try {
                    domainId = Long.parseLong(mallDomain);
                } catch (Exception e) {
                    logger.error("err:",e);
                }
                SysDomain sysDomain = null;
                if (domainId.longValue() == 0) {
                    //悠哉CMS-红包官方站点
                    sysDomain = sysDomainMapper.selectById(0L, 0L);
                } else {
                    // 自有域名
                    sysDomain = sysDomainMapper.selectById(domainId, merId);
                }
                domain = sysDomain.getSiteDomain();
            }

            cmsGoodListUrl = "[域名]/#/pages_other/orders/index?uid=[商户id]&userId=[用户id]&deviceUniqueId=[设备id]";
            cmsGoodListUrl = cmsGoodListUrl.replace("[域名]", Tools.getStr(domain))
                                            .replace("[商户id]", String.valueOf(merId))
                                            .replace("[用户id]", String.valueOf(userId))
                                            .replace("[设备id]", String.valueOf(deviceUniqueId));

            return cmsGoodListUrl;
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return cmsGoodListUrl;
    }
}
